package com.example.userlogin.controller;

import com.example.userlogin.entity.*;
import com.example.userlogin.service.*;
import java.text.SimpleDateFormat;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

//Controller返回的是视图，使用RestConller返回字符串对象
@RestController

@RequestMapping("/api")
public class ApiController {

    @Autowired
    UserService userService;

//    处理商品ajax请求
    @Autowired
    GoodsService goodsService;

//    处理供应商ajax请求
    @Autowired
    SupplierService supplierService;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    PurchaseService purchaseService;

    @RequestMapping("/login")
    public Integer loginProcess(HttpServletRequest request, @RequestParam String username, @RequestParam String userpwd) {
//确认接受到参数
        if (username.isEmpty()) {
            return 0;
        } else {
//            1.组装一下user实体对象
            User user = new User();
            user.setUsername(username);
            user.setPassword(userpwd);
//            2.调用service的findOne方法去查看数据库是否有前端发送的用户名和密码
            User userdb = userService.findOne(user);
//            3.如果有用户名，而且密码也对，就可以进入主页
//              如果有用户名，但密码不对，进入错误页面
//              用户名不存在，直接进入错误页面
            if (!userdb.getUsername().isEmpty()) {
                if (userdb.getPassword().equals(userpwd)){
                    HttpSession session = request.getSession();
                    session.setAttribute("username",username);
                    return 1;
                }else {
                    return 0;
                }
            }else {
                return -1;
            }
        }
    }

    @RequestMapping("/register")
    public Integer register(@RequestParam String username, @RequestParam String userpwd, @RequestParam String reuserpwd) {
        // 接收到前端发送过来的数据
        System.out.println("接收到了数据----" + username);

        if (username.isEmpty() || userpwd.isEmpty() || reuserpwd.isEmpty()) {
            return 0;
        }

        // 检查两次输入的密码是否一致
        if (!userpwd.equals(reuserpwd)) {
            return -1; // 返回一个负数表示密码不一致
        }

        // 将数据组装成user实体对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(userpwd);
        user.setRights("0");
        user.setStatus("0");

        // 调用service的add方法将对象增加到数据库
        if (userService.add(user) == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping("/goodsDelete")
    public Integer goodsDelete(@RequestParam String id) {
//        根据获取的id组装goods实体对象
        System.out.println("接收到的参数id值是:" + id);
        Goods goods = new Goods();
        goods.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (goodsService.delete(goods) == 1) {
            return 1;
        } else {
            return 0;
        }
    }

//    处理更新操作
    @RequestMapping("/goodsUpdateProcess")
    public Integer goodsUpdateProcess(@RequestParam String id,@RequestParam String name,@RequestParam double price
            ,@RequestParam Integer number,@RequestParam String category,
                                  @RequestParam String supplier){
//        组装一个goods实体对象
        Goods  goods = new Goods();
        goods.setId(Integer.valueOf(id));
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setNumber(Integer.valueOf(number));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        System.out.println(goods);
//        调用service的update方法
        if (goodsService.update(goods)==1){
            return 1;
        }else {
            return 0;
        }
    }

    @RequestMapping("/supplierAddProcess")
    public Integer supplierAddProcess(@RequestParam String name,@RequestParam String address,
                                     @RequestParam String contact_person,@RequestParam String telephone){
        //      组装一个goods实体对象
        Supplier supplier = new Supplier();
        supplier.setName(name);
        supplier.setAddress(address);
        supplier.setContact_person(contact_person);
        supplier.setTelephone(telephone);
        //        调用service的add方法给数据库
        if (supplierService.add(supplier)==1){
            //        如果返回ok，那么就进入商品管理首页
            return 1;
        }else {
            return 0;
        }
    }

    @RequestMapping("/employeeDelete")
    public Integer employeeDelete(@RequestParam String id) {
//        根据获取的id组装goods实体对象
        System.out.println("接收到的参数id值是:" + id);
        Employee employee = new Employee();
        employee.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (employeeService.delete(employee) == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping("/employeeUpdateProcess")
    public Integer employeeUpdateProcess(@RequestParam String id,@RequestParam String name, @RequestParam String telephone,
                                     @RequestParam String hiretime, @RequestParam String jobtitile,
                                     @RequestParam String department, @RequestParam double salary
    ){
//        组装一个实体对象
        Employee  employee = new Employee();
        employee.setId(Integer.valueOf(id));
        employee.setName(name);
        employee.setTelephone(telephone);
//        Double.valueOf(price)
        employee.setHiretime(hiretime);
        employee.setJobtitile(jobtitile);
        employee.setDepartment(department);
        employee.setSalary(Double.valueOf(salary));
        System.out.println(employee);
//        调用service的update方法
        if (employeeService.update(employee)==1){
            return 1;
        }else {
            return 0;
        }

    }

    @RequestMapping("/purchaseDelete")
    public Integer purchaseDelete(@RequestParam String id) {
//        根据获取的id组装goods实体对象
        System.out.println("接收到的参数id值是:" + id);
        Purchase purchase = new Purchase();
        purchase.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (purchaseService.delete(purchase) == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping("/purchaseUpdateProcess")
    public Integer purchaseUpdateProcess(@RequestParam Integer id,@RequestParam String supplier, @RequestParam String name,
                                     @RequestParam String category, @RequestParam Integer number,
                                     @RequestParam double price,@RequestParam String time,
                                     @RequestParam double amount){
//        组装一个goods实体对象
        Purchase  purchase = new Purchase();
        purchase.setId(Integer.valueOf(id));
        purchase.setSupplier(supplier);
        purchase.setName(name);
        purchase.setCategory(category);
        purchase.setNumber(Integer.valueOf(number));
        purchase.setPrice(Double.valueOf(price));
        purchase.setTime(time);
        purchase.setAmount(Double.valueOf(amount));
//        Purchase purchase = new Purchase(Integer.valueOf(id),supplier,name,category,
//                Integer.valueOf(number),Double.valueOf(price),time,Double.valueOf(amount));

//        调用service的update方法
        if (purchaseService.update(purchase)==1){
            return 1;
        }else {
            return 0;
        }
    }

    @RequestMapping("/supplierDelete")
    public Integer supplierDelete(@RequestParam String id) {
//        根据获取的id组装goods实体对象
        System.out.println("接收到的参数id值是:" + id);
        Supplier supplier = new Supplier();
        supplier.setId(Integer.valueOf(id));
//        1.调用service的删除方法
        if (supplierService.delete(supplier) == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping("/supplierUpdateProcess")
    public int supplierUpdateProcess(@RequestParam String id, @RequestParam String name, @RequestParam String address,
                                     @RequestParam String contact_person, @RequestParam String telephone){
//        组装一个goods实体对象
        Supplier supplier = new Supplier();
        supplier.setId(Integer.valueOf(id));
        supplier.setName(name);
        supplier.setAddress(address);
        supplier.setContact_person(contact_person);
        supplier.setTelephone(telephone);
        System.out.println(supplier);
//        调用service的update方法
        if (supplierService.update(supplier)==1){
            return 1;
        }else {
            return 0;
        }

    }

}
